package com.lht.leetcodeDemo;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Eric
 * @version 1.0
 * @date 2019-05-28
 */
public class HappyNumber {

    @Test
    public void test(){
        System.out.println(new HappyNumber().isHappy(19));
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        int sum = mySum(n);
        while (sum != 1) {
            //只有4 不是开心数
            if (!set.add(sum)) {
                return false;
            }
            sum = mySum(sum);
        }
        return true;
    }

    public int mySum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += Math.pow(n % 10, 2);
            n /= 10;
        }
        return sum;
    }
}
